//
//  LKHomeNoticeViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/3.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKHomeNoticeViewController.h"
#import "LKHomeNoticeCell.h"
#import "LKHomeNoticeDetailViewController.h"
#import "LKYunYingNoticeModel.h"
@interface LKHomeNoticeViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)UIImage *bluepot;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation LKHomeNoticeViewController

-(LKBaseTableview *)tableview{
    if (!_tableview) {
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain registercellClasss:@[@"LKHomeNoticeCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.emptyDataSetDelegate = self;
        _tableview.emptyDataSetSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.estimatedRowHeight = 200;
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tableview.estimatedRowHeight = 200;
    [self getAllData];
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"公告";
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKHomeNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKHomeNoticeCell"];
    cell.iconImageView.image = self.bluepot;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LKHomeNoticeDetailViewController *vc = [[LKHomeNoticeDetailViewController alloc] init];
    LKYunYingNoticeModel *model = self.dataArray[indexPath.row];
    vc.noticeId = model.noticeId;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}
-(UIImage *)bluepot{
    if (!_bluepot) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        view.backgroundColor = COLOR_HEX(0x3677F7);
        view.layer.cornerRadius = 2.5;
        view.layer.masksToBounds = YES;
        _bluepot = [self makeImageWithView:view withSize:CGSizeMake(5, 5)];
    }
    return _bluepot;
}

/**
 获取数据
 */
-(void)getAllData{
    NSDictionary *param = @{@"MaxResultCount":@"10",@"PageIndex":@"1"};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:homeGetAllYunyingUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSArray *array = responseObject[@"result"][@"items"];
        for (NSDictionary *dic in array) {
            LKYunYingNoticeModel *model = [LKYunYingNoticeModel modelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableview reloadData];
        
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
